<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/7
 * Time: 13:51
 */
namespace app\mtadminapi\controller;

use app\command\controller\Common;
use app\mtadminapi\model\MtBhArea;
use app\mtadminapi\model\MtAdminSysAccount;
use app\mtadminapi\model\MtAdminSysShopExam;
use app\mtadminapi\model\MtShopHouseAsk;
use app\mtadminapi\model\MtShopHouseInstall;
use app\util\ReturnCode;
use app\util\Tools;
use think\console\Command;
use think\Db;
use think\db\Query;
use think\facade\Hook;
use think\Request;
use app\mtadminapi\model\MtShopHouse;
use app\mtadminapi\model\MtShopHouseBedtype;
use app\mtadminapi\model\MtShopHouseTypeitem;

/**
 * 后台管理系统房间管理
 * Class AdminSysRoomManage
 * @package app\mtadminapi\controller
 */
class AdminSysRoomManage extends Base
{
    public function __construct(){
        parent::__construct();
    }
    public //验证规则
        $rule = [
        'shopID'   => 'require|number',
        'area' => 'require|max:15',
        'houseTypeItem' => 'require|max:30',
        'houseName' => 'require|max:35',
        'houseAddress' => 'require|max:100',
        'bedSize' => 'require|max:30',
        'houseSquare' => 'number',
        'modelBedroom' => 'number',
        'modelToilet' => 'number',
        'modelKitchen' => 'number',
        'modelStudy' => 'number',
        'modelBalcony' => 'number',
        'houseItem'  =>  'require',
        'intoTime' => 'require',
        'outTime' => 'require',
        'allowSex' => 'number',
        'allowChildren'   =>  'number',
        'allowOld'   =>  'number',
        'cleanRate'   =>  'number',
        'sheetsRate'   =>  'number',
        'houseNum'      => 'number',
        'sameTypeNumber'      => 'number',
        'reserveDay'      => 'number',
        'isDeposit'      => 'number',
        'refundDay'      => 'number',
        'falsify'      => 'number',
        'houseInstall' => 'require|max:150'
    ];
    /**
     * 房屋添加
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function roomAdd(){
        //提示信息
        $msg = [];
        $data = $this->request->post();
        $verifiRes = (new Common())->VerifiString($this->rule,$msg,$data);
        if($verifiRes['code'] == 2){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,$verifiRes['msg']);
        }
        $data['adminID'] = $this->request->param("adminID");
        $sqlRes = (new MtShopHouse())->insert($data,true);
        if(!$sqlRes){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"入库失败");
        }
        return $this->bindSuccess("入库成功");
    }

    /**
     * 房间照片上传
     */
    public function roomPhotoUpload(){
        if(!$this->request->file("roomPhoto")){
            return $this->bindError(ReturnCode::NOT_EXISTS,"文件不存在");
        }
        $roomPhoto = (new Common())->upload(config("apiadmin.ADMIN_SHOP_LOGO"),"roomPhoto");  // 商家logo
        return $roomPhoto;   // $todo
    }

    /**
     * 房间详情查询
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHouseInfo(){
        $houseID = intval($this->request->param("houseID"));
        if(!$houseID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"请传入houseID");
        }
        $roomArr = (new MtShopHouse())->where([["houseID",'=',$houseID]])->find();
        if(!$roomArr){
            return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        $roomArr->toArray();
        $roomArr = $this->houseHandle($roomArr);
        return $this->bindSuccess("查询成功",$roomArr);
    }

    /**
     * 房间数据处理
     * @param $roomArr
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function houseHandle($roomArr){
        // 地址处理
        $area = $roomArr['area'];
        $roomArr['area'] = $this->areaHandle($area);
        // 床型号处理
        $bedSize = $roomArr['bedSize'];
        $bed = $this->bedSizeHandle($bedSize);
        unset($roomArr['bedSize']);
        $roomArr['bedSize'] = $bed;
        //设施处理
        $roomArr['houseInstall'] = $this->installHandle($roomArr['houseInstall']);
        $roomArr['houseTypeItem'] = $this->houseTypeHandle($roomArr['houseTypeItem']);
        return $roomArr;
    }

    /**
     * 获取房间列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHouseList(){
        $adminID = intval($this->request->get("adminID"));
        $p = intval($this->request->get("p")) ? intval($this->request->get("p")) : 1;
        $limit = intval($this->request->get("limit")) ? intval($this->request->get("limit")) : config("adminapi.ADMIN_LIST_DEFAULT");
        if($adminID){
            $where = [["adminID","=",$adminID],["status","=","1"]];
        }else{
            $where = "1=1";
        }
        $houseList = (new MtShopHouse())->where($where)->page($p,$limit)->select();
        if(!$houseList){
            return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        $houseList = $houseList->toArray();
        foreach ($houseList as $k => $v){
            $houseList[$k] = $this->houseHandle($v);
        }
        return $this->bindSuccess("查询成功",$houseList);
    }

    /**
     * 房间入住要求列表
     */
    public function houseAsk(){
        $ask = (new MtShopHouseAsk())->field("id,askName")->where("status","1")->select();
        if(!$ask){
            return $this->bindError(ReturnCode::DB_READ_ERROR,"要求查找失败");
        }
        return $this->bindSuccess("查询成功",$ask);
    }

    /**
     * 房间类型
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHouseTypeList(){
        $houseTypeList = (new MtShopHouseTypeitem)->where("status","1")->select();
        if(!$houseTypeList)  return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败",$houseTypeList);
        $houseTypeList->toArray();
        $houseTypeList = (new Common())->recursion($houseTypeList,"parentID","typeID");
        return $this->bindSuccess("查询成功",$houseTypeList);
    }
    /**
     * 房间床类型
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getBedTypeList(){
        $houseTypeList = (new MtShopHouseBedtype())->where("status","1")->select();
        if(!$houseTypeList)  return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败",$houseTypeList);
        $houseTypeList->toArray();
        $houseTypeList = (new Common())->recursion($houseTypeList);
        return $this->bindSuccess("查询成功",$houseTypeList);
    }

    /**
     * 房间设施
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHouseInstall(){
        $houseTypeList = (new MtShopHouseInstall())->where("status","1")->select();
        if(!$houseTypeList)  return $this->bindError(ReturnCode::DB_READ_ERROR,"查询失败",$houseTypeList);
        $houseTypeList->toArray();
        $houseTypeList = (new Common())->recursion($houseTypeList);
        return $this->bindSuccess("查询成功",$houseTypeList);
    }

    /**
     * 房屋信息修改
     * @return array
     */
    public function houseDataSave(){
        $msg = [];
        $houseData = $this->request->post();
        $verifiRes = (new Common())->VerifiString($this->rule,$msg,$houseData);
        if($verifiRes['code'] == 2){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,$verifiRes['msg']);
        }
        $res = (new MtShopHouse())->save($houseData);
        if(!$res){
            return $this->bindError(ReturnCode::DB_SAVE_ERROR,"修改失败");
        }
        return $this->bindSuccess("修改成功");
    }
}